package team.nmsg.ge.system.util;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import team.nmsg.ge.system.AppServerConstant;
import team.nmsg.ge.system.exception.NMSIllegalOperationException;
import team.nmsg.ge.system.init.shiro.SysAuthorizingRealm;


@Component
public class SysLanguageHelper {
	
	public static Locale current = Locale.SIMPLIFIED_CHINESE;
	
	private static SysLanguageHelper helper = null;
	
	@Autowired
	private MessageSource messageSource;
	
	public SysLanguageHelper(){
		helper = this;
		current = ServerParams.locale;
	}
	
	public String getMsg1( String msg ){
			return getMsg1(msg , null ,  current);
	}
	
	public String getMsg1( String msg , Object[] args , Locale locale ){
		try {
			String newmsg = messageSource.getMessage( msg , args, locale);
			return newmsg;
		} catch (NoSuchMessageException e) {
		}
		return msg;
	}
	
	public String getMsg1( String msg , Object[] args){
		return getMsg1(msg , args ,  current);
	}
	
	public String getMsg1( String msg , Locale locale ){
		return getMsg1(msg , null ,  locale);
	}
	
	
	//===========================================
	
	
	public static String getMsg( String msg , Object[] args , Locale locale ){
		if( helper == null ){
			return msg;
		}
		return helper.getMsg1(msg, args, locale);
	}


	public static String getMsg( String msg , Object[] args ){
		
		Locale clocale = current;
//		try {
//			clocale = (Locale) SysAuthorizingRealm.
//				getShiroSessionAttr(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
//		} catch (Exception e) { }
		
		return getMsg(msg, args, ServerParams.ZH_CN);
	}
	
	public static String getMsg( String msg , Locale locale ){
		return getMsg(msg, null, locale);
	}
	
	public static String getMsg( String msg){
//		Locale clocale = current;
//		try {
//			clocale = (Locale) SysAuthorizingRealm.
//				getShiroSessionAttr(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
//		} catch (Exception e) { }
//		
//		return getMsg(msg, null, clocale);
		return null;
	}
	
	public static String getMsg( String msg , Object[] args , HttpServletRequest req){
		Locale clocale = current;
		try {
			clocale = getLocalFromReq(req);
		} catch (Exception e) { }
		return getMsg(msg, args, clocale);
	}
	private static Locale getLocalFromReq( HttpServletRequest req) throws  NMSIllegalOperationException{

		String header = req.getHeader( AppServerConstant.REQ_LOCAL_KEY );
		if( header == null  ){
			header = req.getParameter( AppServerConstant.REQ_LOCAL_KEY );
		}
		if(header==null){
			throw new NMSIllegalOperationException("Illegal Operation!");
		}
		if(ServerParams.LOCAL_ZH_CN_STR.equals(header)){
			return ServerParams.ZH_CN;
		}else {
			return ServerParams.EN_US;
		}
	}
	
}
